package cc.mycraft.manager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Location;

import cc.mycraft.LogType;
import cc.mycraft.MYCLog;
import cc.mycraft.events.PlayerLogEvent;

public class PlayerLogWriter {
	
	private static File LogFile;
	private static String Today;
	private static final String SP = "|";
	public static boolean showBukkit = false;
	private static int Amount = 0;
	private static List<String> Log = new LinkedList<String>();
	
	public static void run(){
		Today = String.format("%tF", new Date());
		String FolderPath = MYCLog.getInstance().getDataFolder()+"/"+Today.substring(0,7)+"/";
		LogFile = new File(FolderPath+Today+".log");
		File Folder = new File(FolderPath);
		try {
			if(!MYCLog.getInstance().getDataFolder().exists()) {
				MYCLog.getInstance().getDataFolder().mkdir();
			}
			if(!Folder.exists()) {
				Folder.mkdir();
			}
			if(!LogFile.exists()) {
				LogFile.createNewFile();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void add(byte logType,String obj,Location objloc,String actType,String actobj,Location actloc,String Extra,boolean show) {
		Amount++;
		String pre = Amount+SP+logType+SP+String.format("%tT",new Date().getTime())+SP;
		String log = obj+SP+getLocStr(objloc)+SP+actType+SP+actobj+SP+getLocStr(actloc)+SP+Extra;
		if(show) {Bukkit.getLogger().info(log);}
		Log.add(pre+log+"\n");
	}
	public static void addLog(String obj,Location objloc,String actType,String actobj,Location actloc,String Extra) {
		Amount++;
		String pre = Amount+SP+LogType.LOG+SP+String.format("%tT",new Date().getTime())+SP;
		String log = obj+SP+getLocStr(objloc)+SP+actType+SP+actobj+SP+getLocStr(actloc)+SP+Extra;
		if(showBukkit) {Bukkit.getLogger().info(log);}
		Log.add(pre+log+"\n");
	}
	public static void addPLog(String obj,Location objloc,String actType,String actobj,Location actloc,String Extra) {
		Amount++;
		String pre = Amount+SP+LogType.PLOG+SP+String.format("%tT",new Date().getTime())+SP;
		String log = obj+SP+getLocStr(objloc)+SP+actType+SP+actobj+SP+getLocStr(actloc)+SP+Extra;
		Bukkit.getLogger().info(log);
		Log.add(pre+log+"\n");
	}
	public static void addCLog(String obj,Location objloc,String actType,String actobj,Location actloc,String Extra) {
		Amount++;
		String pre = Amount+SP+LogType.CLOG+SP+String.format("%tT",new Date().getTime())+SP;
		String log = obj+SP+getLocStr(objloc)+SP+actType+SP+actobj+SP+getLocStr(actloc)+SP+Extra;
		Bukkit.getLogger().info(log);
		Log.add(pre+log+"\n");
	}
	private static String getLocStr(Location l) {
		if(l==null) {
			return "NONE";
		}
		return l.getWorld().getName()+","+l.getBlockX()+","+l.getBlockY()+","+l.getBlockZ();
	}


	public static void addLog(PlayerLogEvent e) {
		Amount++;
		String pre = Amount+SP+e.getLogType()+SP+String.format("%tT", e.getTime())+SP;
		String log = e.getObj()+SP+e.getObjLoc()+SP+e.getAction()+SP+e.getActObj()+SP+e.getActLoc()+SP+e.getExtra();
		Log.add(pre+log+"\n");
		if(e.isShowBukkit()) {
			Bukkit.getLogger().info(log);
		}
	}

	
	public static void saveLog(){
		Bukkit.getScheduler().runTaskAsynchronously(MYCLog.getInstance(), ()->{
			writeAllLogs();
		});
	}
	public static void writeAllLogs() {
		try {
			writeLog();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Bukkit.getLogger().info("Log Saved");
	}
	
	private static void writeLog() throws IOException {
		if(Log.isEmpty()) {
			return;
		}
		FileOutputStream out = new FileOutputStream(LogFile,true);
		for(int i=0;i<Log.size();i++) {
			StringBuffer sb = new StringBuffer();
			sb.append(Log.get(i));
			out.write(sb.toString().getBytes("utf-8"));
		}
		out.close();
		Log.clear();
	}

	
}
